package me.xiaosiqi.syt.hosp.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import me.xiaosiqi.syt.common.exception.HospitalException;
import me.xiaosiqi.syt.common.result.Result;
import me.xiaosiqi.syt.common.result.ResultCodeEnum;
import me.xiaosiqi.syt.hosp.service.HospitalSetService;
import me.xiaosiqi.syt.model.hosp.HospitalSet;
import me.xiaosiqi.syt.vo.hosp.HospitalQueryVo;
import me.xiaosiqi.syt.vo.hosp.HospitalSetQueryVo;
import org.apache.tomcat.util.security.MD5Encoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import sun.security.provider.MD5;

import java.util.List;
import java.util.Random;

/**
 * @author xiaosiqi
 * @date 2022/1/25 23:02
 */
@Api(tags = "医院设置管理")
@CrossOrigin
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
public class HospitalSetController {
    @Autowired
    private HospitalSetService hospitalSetService;

    @ApiOperation("获取所有医院信息")
    @GetMapping("findAll")
    public Result findAll() {
        return Result.ok(hospitalSetService.list());
    }

    @ApiOperation("逻辑删除医院信息")
    @DeleteMapping("{id}")
    public Result removeById(@PathVariable Long id) {
        boolean isOk = hospitalSetService.removeById(id);
        if (isOk) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    @ApiOperation("分页条件查询")
    @PostMapping("findPage/{current}/{limit}")
    public Result findPage(@PathVariable long current,
                           @PathVariable long limit,
                           @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo) {
        return Result.ok(hospitalSetService.findPage(current, limit, hospitalSetQueryVo));
    }

    @ApiOperation("添加医院")
    @PostMapping("save")
    public Result save(@RequestBody HospitalSet hospitalSet) {
        //设置状态 1 可用， 0不能使用
        hospitalSet.setStatus(1);
        //设置签名密钥
        Random random = new Random();
        String signKey = MD5Encoder.encode((System.currentTimeMillis() + "" + random.nextInt(1000)).getBytes());
        hospitalSet.setSignKey(signKey);
        boolean isOk = hospitalSetService.save(hospitalSet);
        if (isOk) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    @ApiOperation("根据id获取医院设置")
    @GetMapping("{id}")
    public Result getById(@PathVariable long id) {
        return Result.ok(hospitalSetService.getById(id));
    }

    @ApiOperation("根据id修改医院设置")
    @PostMapping("updateById")
    public Result updateById(@RequestBody HospitalSet hospitalSet) {
        boolean isOk = hospitalSetService.updateById(hospitalSet);
        if (isOk) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    @ApiOperation("根据id批量删除")
    @PostMapping("removeByIds")
    public Result removeByIds(@RequestBody List<Long> ids) {
        boolean isOk = hospitalSetService.removeByIds(ids);
        if (isOk) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    @ApiOperation("医院设置锁定和解锁")
    @PutMapping("lock/{id}/{status}")
    public Result lock(@PathVariable long id, @PathVariable int status) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        hospitalSet.setStatus(status);
        hospitalSetService.updateById(hospitalSet);
        return Result.ok();
    }

    @ApiOperation("短信发送签名密钥")
    @PutMapping("sendKey/{id}")
    public Result sendKey(@PathVariable long id) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        String signKey = hospitalSet.getSignKey();
        String hoscode = hospitalSet.getHoscode();
        //TODO: 后续完善短信发送
        return Result.ok();
    }
}
